

const express = require('express');
const router = express.Router();
const phantom = require('phantom');
const cheerio = require('cheerio');

/* GET home page. */
router.get('/', function (req, res, next) {
    res.header('Content-Type', 'application/json');
    let sitepage = null; //创建网页对象实例
    let phInstance = null; //创建phantomj实例对象
    phantom.create()
        .then(instance => {
            phInstance = instance;
            return instance.createPage();
        })
        .then(page => {
            sitepage = page;
            return page.open('https://www.baidu.com/');
        })
        .then(status => {
            console.info(status); //获取结果状态
            return sitepage.property('content'); //获取相应的属性内容
        })
        .then(content => {
            const $ = cheerio.load(content);  //解析输出的结果内容
            const jsonResult = [];
            $('a[href]').each((i, item) => {  //抓取符合条件的a标签的链接地址
                const href = $(item).attr('href');
                if (new RegExp(/http[s]?:\/\/.*/).test(href)) {
                    jsonResult.push(href);
                }
            });
            sitepage.close();
            phInstance.exit();
            res.json(jsonResult);
        })
        .catch(error => {
            console.log(error);
            phInstance.exit();
            res.json({status: false});
        });
});

//  module.exports = router;


var server = router.listen(3000, function() {
  console.log('listening at 3000');

//  module.exports = router;
});